package com.zj.core.base

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.Gravity
import android.view.View
import com.dialogutils.DialogUtils
import com.dialogutils.ToastUtil
import com.fileutils.SPUtils
import com.systemutils.SystemUtil
import com.zj.core.R
import com.zj.core.activity.AboutActivity
import com.zj.core.activity.HelpMesActivity
import com.zj.core.activity.LoginActivity
import com.zj.core.activity.RefuseActivity
import com.zj.core.bean.UserInfo
import com.zj.core.dialog.SearchDialog
import com.zj.core.dialog.TextSizeDialog
import com.zj.core.interfaces.TextMessageIml
import com.zj.core.net.NetPresenter
import com.zj.core.net.NetValueCallBackIml
import com.zj.core.services.BleToothService
import com.zj.core.tools.Constant.Companion.MODEL_VOICE_KEY
import com.zj.core.tools.Constant.Companion.NOTIFY_VOICE_KEY
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*


/**
 * 日期：2019年6月19日
 * 公司：北京子杰宝贝科技有限公司
 * 地址：北京市朝阳区朝阳农场
 * 开发者：达选文
 * <p>
 * 主体功能：主界面基本功能
 */
@SuppressLint("Registered")
open class BaseDrawerActivity
    : BaseActivity(),
    View.OnClickListener {

    private var mBackKeyPressed = false     // 记录是否有首次按键

    var hospitalId: String = ""                 // 医院ID
    var departmentId: String = ""               // 科室ID
    var nurseId: String = ""                    // 护士ID

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        setTitleBarParams(title_bar_bg_clt)  //设置标题栏高度
        initView()

        // 查询医院ID，科室ID
        NetPresenter().getApplyCationConfig(
            cationCinfigIml,
            SystemUtil.getIMEI(BaseApplication.context)
        )
    }

    @SuppressLint("SetTextI18n")
    open fun initView() {
        nurseId = intent.getStringExtra("nurseId")
        title_bar_tv.text = "当前护士ID:\n${nurseId.replace("\n", "")}"
        user_id_tv.text = nurseId.replace("\n", "")

        voice_notice_bu.isChecked = SPUtils.getVoiceStatus(BaseApplication.context,NOTIFY_VOICE_KEY)
        val modelStatus=SPUtils.getModelStatus(BaseApplication.context,MODEL_VOICE_KEY)
        if(modelStatus==1){
            asr_voice_model_bu.isChecked=true
        }else if(modelStatus==2){
            asr_voice_model_bu.isChecked=false
        }

        title_bar_back_img.setOnClickListener(this)
        float_action_bu.setOnClickListener(this)
        drawer_refuse_tv.setOnClickListener(this)
        drawer_about_tv.setOnClickListener(this)
        drawer_logout_tv.setOnClickListener(this)
        voice_notice_lly.setOnClickListener(this)
        asr_voice_model.setOnClickListener(this)
        operate_explain.setOnClickListener(this)
        text_size_main_view.setOnClickListener(this)
    }

    @SuppressLint("RtlHardcoded")
    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.title_bar_back_img -> {                //设置
                drawer_layout.openDrawer(Gravity.LEFT)
            }

            R.id.text_size_main_view -> {               //字体大小
                TextSizeDialog(this@BaseDrawerActivity).show()
            }

            R.id.float_action_bu -> {                  //搜索
                SearchDialog(this@BaseDrawerActivity,
                    object : TextMessageIml {
                        override fun messageResult(str: String) {
                            searchRecord(str)
                        }
                    }).show()
            }

            R.id.drawer_refuse_tv -> {                 //反馈界面
                drawer_layout.closeDrawer(Gravity.LEFT)
                activityJump(this, RefuseActivity().javaClass, Bundle())
            }

            R.id.operate_explain -> {                   //操作说明
                drawer_layout.closeDrawer(Gravity.LEFT)
                activityJump(this, HelpMesActivity().javaClass, Bundle())
            }

            R.id.drawer_about_tv -> {                  //关于界面
                drawer_layout.closeDrawer(Gravity.LEFT)
                activityJump(this, AboutActivity().javaClass, Bundle())
            }

            R.id.drawer_logout_tv -> {                  //退出
                DialogUtils.create(
                    this, null, "确认退出？", "退出",
                    mViewListener, "取消", mViewListener, true, true,
                    false
                ).show()
            }

            R.id.voice_notice_lly -> {
                voice_notice_bu.isChecked = !voice_notice_bu.isChecked
                if (voice_notice_bu.isChecked) {
                    SPUtils.put(BaseApplication.context, NOTIFY_VOICE_KEY,1)
                    ToastUtil.showShort(BaseApplication.context, "已打开提示音")
                } else {
                    SPUtils.put(BaseApplication.context, NOTIFY_VOICE_KEY,2)
                    ToastUtil.showShort(BaseApplication.context, "已关闭提示音")
                }
            }

            R.id.asr_voice_model->{                      //语料模型开关
                asr_voice_model_bu.isChecked = !asr_voice_model_bu.isChecked
                if (asr_voice_model_bu.isChecked) {
                    SPUtils.put(BaseApplication.context, MODEL_VOICE_KEY,1)
                    ToastUtil.showShort(BaseApplication.context, "已开启模型")
                } else {
                    SPUtils.put(BaseApplication.context, MODEL_VOICE_KEY,2)
                    ToastUtil.showShort(BaseApplication.context, "已关闭模型")
                }
            }
        }
    }

    /**
     * 获取医院ID，科室ID
     */
    private var cationCinfigIml = object : NetValueCallBackIml {
        override fun onComplete() {}
        override fun onSubscribe(data: Any?) {}
        override fun onNext(values: Any?, bean: Any?) {
            hospitalId = bean as String
            departmentId = values as String
            UserInfo.departmentID=departmentId
            UserInfo.hospitalID=hospitalId
            requestNurseHis()
        }

        override fun onError() {
            ToastUtil.showShort(BaseApplication.context, "科室配置信息获取失败")
        }
    }

    /**
     * 退出点击事件监听
     */
    private var mViewListener: View.OnClickListener = View.OnClickListener { v ->
        when (v.id) {
            R.id.common_dialog_cancel_tv -> {
            }

            R.id.common_dialog_confirm_tv -> finish()
        }
    }
    /**
     * 用户注销点击事件监听
     */
    private var mLoginOutListener: View.OnClickListener = View.OnClickListener { v ->
        when (v.id) {
            R.id.common_dialog_cancel_tv -> {
            }

            R.id.common_dialog_confirm_tv -> {
                activityJump(this, LoginActivity().javaClass, Bundle())
                finish()
            }
        }
    }



    /**
     * 搜索
     */
    open fun searchRecord(str: String) {}

    /**
     * 请求历史记录
     */
    open fun requestNurseHis() {}
    override fun maorenClick() {}
    override fun centerClick() {}
    override fun rightClick() {
        doubleClick()
    }

    override fun leftClick() {
        doubleClick()
    }

    /**
     * 双击退出
     */
    private fun doubleClick() {
        if (!mBackKeyPressed) {
            ToastUtil.showShort(this, "再按一次退出程序")
            mBackKeyPressed = true
            Timer().schedule(object : TimerTask() {
                override fun run() {
                    mBackKeyPressed = false
                }
            }, 2000)
        } else {
            disConnectBle(BleToothService.mConnectMac.toString())
            finish()
        }
    }
}